package top.codedance.iotp.serialPortSDK.util;


import org.apache.commons.lang3.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
* @author:
* @date:
*/
public class DateUtil {
    public static final SimpleDateFormat FORMATTER_YEAR = new SimpleDateFormat("yyyy");
    public static final SimpleDateFormat FORMATTER_MON = new SimpleDateFormat("yyyy-MM");
    public static final SimpleDateFormat FORMATTER_DD = new SimpleDateFormat("yyyy-MM-dd");
    public static final SimpleDateFormat FORMATTER_SS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final SimpleDateFormat FORMATTER_TSS = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    public static final SimpleDateFormat FORMATTER_MM = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    public static final DateFormat FORMATTER_T = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    /**
     * 中文星期
     */
    public static final String SUNDAY = "星期日";
    public static final String MONDAY = "星期一";
    public static final String TUESDAY = "星期二";
    public static final String WEDNESDAY = "星期三";
    public static final String THURSDAY = "星期四";
    public static final String FRIDAY = "星期五";
    public static final String SATURDAY = "星期六";

    /**
     * 根据format类型得到时间对象的字符串格式
     *
     * @param date
     * @param format
     * @return
     */
    public static String getStrByFormat(Date date, DateFormat format) {
        return format.format(date);
    }

    /**
     * 获取当前系统时间
     */
    public static String getCurDateTime() {
        return getStrByFormat(new Date(), FORMATTER_SS);

    }

    /**
     * 获取当前系统时间年月
     */
    public static String getCurDateMonth() {
        return getStrByFormat(new Date(), FORMATTER_MON);
    }

    /**
     * 获取当前系统时间年
     */
    public static String getCurDateYear() {
        return getStrByFormat(new Date(), FORMATTER_YEAR);
    }

    /**
     * 根据format类型将字符串转换为Date类型
     *
     * @param dateStr
     * @param format
     * @return
     */
    public static Date getDateByFormat(String dateStr, DateFormat format) {
        Date date = null;
        try {
            date = format.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 某个日期加减天数
     *
     * @param date
     * @param days
     * @return
     */
    public static Date addAndSubDays(Date date, int days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_YEAR, days);
        return cal.getTime();
    }

    /**
     * 设置日期的时分秒
     *
     * @param date
     * @param hmsStr
     * @return
     */
    public static Date setHMS(Date date, String hmsStr) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        String[] hmsStrs = hmsStr.split(":");
        cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hmsStrs[0]));
        cal.set(Calendar.MINUTE, Integer.parseInt(hmsStrs[1]));
        cal.set(Calendar.SECOND, Integer.parseInt(hmsStrs[2]));
        return cal.getTime();
    }

    /**
     * 计算星期几
     */
    public static int getWeekDay(Date today) {
        Calendar c = Calendar.getInstance();
        c.setTime(today);
        int weekday = c.get(Calendar.DAY_OF_WEEK);
        return weekday;
    }

    /**
     * 查当前日期是一周中的第几天、星期几
     *
     * @return
     */
    public static long getWhicthDay(Date today) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(today);
        // 1=Sunday,2=Monday,,,7=Saturday。
        return cal.get(Calendar.DAY_OF_WEEK);
    }

    /**
     * 比较两个日期是否为同一年，如果为同一年返回true
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isEqualYear(Date date1, Date date2) {
        if (getYear(date1) == getYear(date2)) {
            return true;
        }
        return false;
    }

    /**
     * 判断日期是不是今天
     * @param date
     * @return    是返回true，不是返回false
     */
    public static boolean isNow(Date date) {
        // 默认的年月日的格式. yyyy-MM-dd
        // 当前时间
        Date now = new Date();
        //获取今天的日期
        String nowDay = FORMATTER_DD.format(now);
        //对比的时间
        String day = FORMATTER_DD.format(date);
        return day.equals(nowDay);
    }

    /**
     * 比较两个时间大小，1大于2是返回1，等于返回0，小于返回-1
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int compareTo(Date date1, Date date2) {
        if (date1.getTime() == date2.getTime()) {
            return 0;
        } else if (date1.getTime() > date2.getTime()) {
            return 1;
        } else {
            return -1;
        }
    }

    /**
     * 提前几秒钟方法
     *
     * @param date   基准时间
     * @param second 需提前秒数
     * @return
     */
    public static Date advanceSecond(Date date, long second) {
        long resultDate = date.getTime() - second * 1000;
        return new Date(resultDate);
    }

    /**
     * 获取某个日期的年份
     *
     * @param date
     * @return
     */
    public static int getYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.YEAR);
    }

    /**
     * 格式化为固定格式的字符串
     *
     * @param dateStr
     * @return
     */

    public static String formatDateString(String dateStr) {
        if (StringUtils.isBlank(dateStr)) {
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date tDate = format.parse(dateStr, new ParsePosition(0));
        return format.format(tDate);
    }

    /**
     * 自定义format串码
     *
     * @param formarStr
     * @return
     */

    public static String formatDate(String formarStr) {
        SimpleDateFormat dateFormator = new SimpleDateFormat(formarStr);
        return dateFormator.format(new Date());
    }

    public static String getFormatedDateTime(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat dateFormator = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormator.format(date);
    }

    public static String formatDefaultDateTime(Date date) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    public static String getFormatedDateTimeDay(Date date) {
        SimpleDateFormat dateFormator = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormator.format(date);
    }

    /**
     * 获取系统当前日期
     *
     * @return
     */
    public static String getFormatedDate() {
        Date date = getCurrentDate();
        SimpleDateFormat dateFormator = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormator.format(date);
    }

    /**
     * 获取当前周几
     */
    public static String getWeekTime() throws Exception {
        Date date = new Date();
        SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
        String currSun = dateFm.format(date);
        return currSun;
    }

    public static Date getCurrentDate() {
        return new Date(System.currentTimeMillis());
    }

    /**
     * 获取传入时间为周几
     */
    public static String getWeekTimeChina(Date date) throws Exception {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 1=Sunday,2=Monday,,,7=Saturday
        int week = cal.get(Calendar.DAY_OF_WEEK);
        String w = StringUtils.EMPTY;
        switch (week) {
            case 1:
                w = SUNDAY;
                break;
            case 2:
                w = MONDAY;
                break;
            case 3:
                w = TUESDAY;
                break;
            case 4:
                w = WEDNESDAY;
                break;
            case 5:
                w = THURSDAY;
                break;
            case 6:
                w = FRIDAY;
                break;
            case 7:
                w = SATURDAY;
                break;
            default:
                break;
        }
        return w;
    }

    public static Date parseStrToDateAndTime(String dateStr) throws Exception {
        if (null != dateStr && !"".equals(dateStr)) {
            SimpleDateFormat dateFormator;
            Date tDate = new Date();
            if (dateStr.length() == 14) {
                dateFormator = new SimpleDateFormat("yyyyMMddHHmmss");
                tDate = dateFormator.parse(dateStr);
            }else {
                dateFormator = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                tDate = dateFormator.parse(dateStr);
            }
            return tDate;
        } else {
            return null;
        }
    }

    /**
     * 拿到date2减去date1有多少分钟
     * @param date1
     * @param date2
     * @return
     */
    public static long getMinute(Date date1, Date date2) {
        return (date2.getTime() - date1.getTime()) / (1000 * 60);
    }

    public static Date parseStrToDate(String dateStr) throws Exception {
        if (null != dateStr && !"".equals(dateStr)) {
            SimpleDateFormat dateFormator = new SimpleDateFormat("yyyy-MM-dd");
            Date tDate = dateFormator.parse(dateStr);
            return tDate;
        } else {
            return null;
        }
    }

    public static Date parseStrToDateEnd(String dateStr) throws Exception {
        if (null != dateStr && !"".equals(dateStr)) {
            dateStr = dateStr + " 23:59:59";
            SimpleDateFormat dateFormator = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date tDate = dateFormator.parse(dateStr);
            return tDate;
        } else {
            return null;
        }
    }

    /**
     * 获取本周开始时间
     *
     * @param
     */
    public static Date getTimesWeekmorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return cal.getTime();
    }


    /**
     * 获取本周结束时间
     *
     * @param
     */
    public static Date getTimesWeeknight() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getTimesWeekmorning());
        cal.add(Calendar.DAY_OF_WEEK, 7);
        return cal.getTime();
    }

    /**
     * 获取指定年月的第一天
     * @param year
     * @param month
     * @return
     */
    public static String getFirstDayOfMonth1(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最小天数
        int firstDay = cal.getMinimum(Calendar.DATE);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());
    }

    /**
     * 获取指定年月的最后一天
     * @param year
     * @param month
     * @return
     */
    public static String getLastDayOfMonth1(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DATE);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());
    }


    /**
     * @return java.util.Date
     * @Description 将时分秒赋值成日结时间23:59:59
     * @Param date
     * @date 2021/1/31 17:33
     */
    public static Date transateDateTODateEnd(Date date) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date);
        //
        cal1.set(Calendar.HOUR_OF_DAY, 23);
        cal1.set(Calendar.MINUTE, 59);
        cal1.set(Calendar.SECOND, 59);
        return cal1.getTime();
    }

    /**
     * @return java.util.Date
     * @Description 将时分秒赋值成日初始时间 00:00:00
     * @Param date
     * @date 2021/1/31 17:35
     */
    public static Date transateDateTODateStart(Date date) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date);
        // 将时分秒,毫秒域清零
        cal1.set(Calendar.HOUR_OF_DAY, 00);
        cal1.set(Calendar.MINUTE, 00);
        cal1.set(Calendar.SECOND, 00);
        return cal1.getTime();
    }

    /**
     * 获得前几分钟时间 如果传-1 表示获得前1分钟时间
     * @param date
     * @param minute
     * @return
     */
    public static Date getBoforeMinute(Date date, Integer minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minute);
        return calendar.getTime();
    }


    /**
     * 日期转字符串
     * @param date
     * @param dateFormat
     * @return
     */
    public static String format(Date date, DateFormat dateFormat) throws ParseException {
        return dateFormat.format(date);
    }

    public static void main(String[] args) {
        System.out.println(getCurDateTime());

    }
}

